package com.shop.mms_client.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.shop.mms_client.R;
import com.shop.mms_client.viewmodel.BlacklistViewModel;

public class BlacklistFragment extends Fragment {
    private BlacklistViewModel viewModel;
    private RecyclerView recyclerView;
    private BlacklistAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blacklist, container, false);
        
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BlacklistAdapter();
        recyclerView.setAdapter(adapter);

        FloatingActionButton fab = view.findViewById(R.id.fabAdd);
        fab.setOnClickListener(v -> showAddDialog());

        viewModel = new ViewModelProvider(this).get(BlacklistViewModel.class);
        viewModel.getBlacklistItems().observe(getViewLifecycleOwner(), items -> {
            adapter.submitList(items);
        });

        return view;
    }

    private void showAddDialog() {
        // 显示添加黑名单项的对话框
        AddBlacklistDialog dialog = new AddBlacklistDialog();
        dialog.show(getChildFragmentManager(), "AddBlacklistDialog");
    }
} 